<?php

require_once('mysqlClass.php');

class wordControl {
  var $mysql;
  var $settings;

  function __construct($username, $server = 'localhost', $database = 'LINDtest', $usergroup = 'LIND', $passwordOfUsergroup = 'LIND') {
    $this->mysql = new mysqlClient($server, $database, $usergroup, $passwordOfUsergroup);
    $this->settings = $this->mysql->getData('SELECT startingLanguage, dictionary FROM settings WHERE username = "'. $username .'"');
    $this->settings = $this->settings[0];
  }
  
  function addWord($localWord, $foreignWord) { //Example syntax of foreignWord: translation1,synonym1;translation2,synonym2,synonym2
    $translations = explode(';', $foreignWord);
    
    foreach ($translations as $translation) {
      $synonyms[] = explode(',', $translation);
    }
    
    //local word
    $this->mysql->putData('INSERT INTO words(word, dictionary, language) VALUES ("'. $localWord .'", "'. $dictionary .'", "English")');
    $localId = $this->mysql->getData('SELECT id FROM words WHERE word = "'. $localWord .'"');
    
    //translation groups, consist of synonyms
    for ($i = 0; $i < count($synonyms); $i++) {
    
      //Breaks synonym groups to words and inserts them
      for ($j = 0; $j < count($synonyms[$i]); $j++) {
        $this->mysql->putData('INSERT INTO words(word, dictionary, translations, language) VALUES ("'. $synonyms[$i][$j] .'", "'. $dictionary .'", "'. $localId[0][0] .' '.'", "Finnish")');
        
        //replaces word with its id
        $id = $this->mysql->getData('SELECT id FROM words WHERE word = "'. $synonyms[$i][$j] .'"');
        $synonyms[$i][$j] = $id[0][0];
      }
    }
    
    //groups
    for ($i = 0; $i < count($synonyms); $i++) {
      //words
      for ($j = 0; $j < count($synonyms[$i]); $j++) {
        //word
        for ($k = 0; $k < count($synonyms[$i]); $k++) {
          if ($synonyms[$i][$k] == $synonyms[$i][$j]) {
            continue;
          }
          
          else {
            $idRow .= $synonyms[$i][$k] .' ';
          }
        }
      
        $this->mysql->putData('UPDATE words SET synonyms = "'. $idRow .'" WHERE id = "'. $synonyms[$i][$j] .'"');
        $idRow = '';
      }
    }
    
    $foreignIds = $this->mysql->getData('SELECT id FROM words WHERE translations = "'. $localId[0][0] .'"');
    
    //update translation ids of the local word
    for ($i = 0; $i < count($foreignIds); $i++) {
      $idRow .= $foreignIds[$i][0] .' ';
    }
    
    $this->mysql->putData('UPDATE words SET translations = "'. trim($idRow) .'" WHERE id = "'. $localId[0][0] .'"');
  }
  
  function getSynonyms($sourceWord, $level = false) {
    $currentSynonyms = $this->mysql->getData('SELECT synonyms FROM words WHERE id = "'. $sourceWord .'"');
    $currentSynonyms = explode(' ', $currentSynonyms[0][0]);
    
    $overallSynonyms = array();

    while (count($currentSynonyms) > 0) {
      foreach ($currentSynonyms as $lowerSynonym) {
        $synonymPack = array();
        
        $lowerSynonyms = $this->mysql->getData('SELECT synonyms FROM words WHERE id = "'. $lowerSynonym .'"');
        $lowerSynonyms = explode(' ', $lowerSynonyms[0][0]);
        
        array_pop($lowerSynonyms);
        
        $synonymPack = array_merge($synonymPack, $lowerSynonyms);
        $overallSynonyms = array_merge($overallSynonyms, $synonymPack);      
      }
      
      $currentSynonyms = $synonymPack;      
    }
    
    return array_unique($overallSynonyms);
  }
}

?>
